package com.ews.onlineexchange.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * 实体名称：行业类别 说明：包含行业大类、小类
 * 
 * @author 司冰
 */
@XmlRootElement
@Entity
@Table(uniqueConstraints = {
		@UniqueConstraint(columnNames = {"sectorName", "parentSector_id"}),
//		@UniqueConstraint(columnNames = {"sno", "parentSector_id"})
})
public class Sector extends DataEntity {
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private String	sectorName;		// 行业类别名称
	private String  sno; 			// 编码（排序查询用）
	private int 	intSno;			// 整型编码，生成编码用
	private Sector	parentSector;	// 上级类别
	private List<Sector> childSectors = new ArrayList<>();
	private Boolean			   certAuditedResult;
	//@Transient serviceDepartment for relate persist qualificationClass
	private ServiceDepartment serviceDepartment;

	@NotNull
	public String getSectorName() {
		return sectorName;
	}
	
	
	@Transient
	public String getLevelSectorName() {
		StringBuilder sb = new StringBuilder();
		for ( int i = 0; i < getLevel(); i++) {
			sb.append("　");
		}
		sb.append(getSectorName());
		return sb.toString();
	}

	public void setSectorName(String sectorName) {
		this.sectorName = sectorName;
	}
	

	@Transient
	public int getSectorLevel() {
		if (this.getParentSector() == null)
			return 0;
		return 1 + this.getParentSector().getSectorLevel();
	}

	@NotNull
	@Column(unique = true)
	public String getSno() {
		return sno;
	}

	public void setSno(String sno) {
		this.sno = sno;
	}

	public int getIntSno() {
		return intSno;
	}

	public void setIntSno(int intSno) {
		this.intSno = intSno;
	}
	
	
	@ManyToOne
	public Sector getParentSector() {
		return parentSector;
	}

	public void setParentSector(Sector parentSector) {
		this.parentSector = parentSector;
	}
	
	@XmlTransient
	@OneToMany(mappedBy = "parentSector",fetch = FetchType.LAZY)
	@OrderBy("sort")
	public List<Sector> getChildSectors() {
		return childSectors;
	}

	public void setChildSectors(List<Sector> childSectors) {
		this.childSectors = childSectors;
	}

	@XmlTransient
	@Transient
	public ServiceDepartment getServiceDepartment() {
		return serviceDepartment;
	}

	public void setServiceDepartment(ServiceDepartment serviceDepartment) {
		this.serviceDepartment = serviceDepartment;
	}
	
	@XmlTransient
	@Transient
	public int getLevel() {
		if (getParentSector() == null)
			return 0;
		return 1 + getParentSector().getLevel();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((parentSector == null) ? 0 : parentSector.hashCode());
		result = prime * result
				+ ((sectorName == null) ? 0 : sectorName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Sector other = (Sector) obj;
		if (parentSector == null) {
			if (other.parentSector != null)
				return false;
		} else if (!parentSector.equals(other.parentSector))
			return false;
		if (sectorName == null) {
			if (other.sectorName != null)
				return false;
		} else if (!sectorName.equals(other.sectorName))
			return false;
		if(!this.getId().equals(other.getId()))return false;
		return true;
	}
	

	@Transient
	public String getFullSectorName() {
		if (this.getParentSector() == null)
			return this.getSectorName();
		else 
			return this.getParentSector().getFullSectorName().concat("-").concat(this.getSectorName());
	}

	@XmlTransient
	@Transient
	public List<Sector> getSectorTree() {
		List<Sector> resultList = null;		
		if (this.getParentSector() == null) {
			resultList = new ArrayList<Sector>();
		} else {
			resultList = this.getParentSector().getSectorTree();			
		}
		resultList.add(this);
		return resultList;
	}
	
	@Transient
	public Boolean getCertAuditedResult() {
		return certAuditedResult;
	}

	public void setCertAuditedResult(Boolean certAuditedResult) {
		this.certAuditedResult = certAuditedResult;
	}
}
